<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>产品信息列表</title>
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="css/user.css">
    <script src="js/user.js"></script>
</head>

<body>

<div class="container-fluid">
    <div class="row">

        <!--右边内容栏-->
        <div class="col-sm-12" id="right">
            <ol class="breadcrumb">
                <li class="active">菜单
                </li>
                <li class="active">编辑设备信息
                </li>
            </ol>

            <div class="panel panel-default">
                <div class="panel-heading">
                    填写设备信息
                </div>
                <div class="panel-body">
                    <%
                        int i = 0;
                        if (request.getParameter("id") != null) {
                            String I = request.getParameter("id");
                            i = Integer.parseInt(I);
                        }
                        String name = "";
                        String number = "";
                        String type = "";
                        String vendor = "";
                        String date = "";
                        String time = "";
                        String afterSales = "";
                        String phone = "";
                        String illustrate="";
                        Connection conn = (Connection)session.getAttribute("Connection");
                        String sql = "select * from device where id=" + i;
                        PreparedStatement preparedStatement = conn.prepareStatement(sql);
                        ResultSet rs = preparedStatement.executeQuery();
                        rs.next();
                        try {
                            if(rs.getString("name") != null)
                                name = rs.getString("name");
                            if(rs.getString("number") != null)
                                number = rs.getString("number");
                            if(rs.getString("type") != null)
                                type = rs.getString("type");
                            if(rs.getString("vendor") != null)
                                vendor = rs.getString("vendor");
                            if(rs.getString("date") != null)
                                date = rs.getString("date");
                            if(rs.getString("time") != null)
                                time = rs.getString("time");
                            if(rs.getString("afterSales") != null)
                                afterSales = rs.getString("afterSales");
                            if(rs.getString("phone") != null)
                                phone = rs.getString("phone");
                            if(rs.getString("illustrate") != null)
                                illustrate = rs.getString("illustrate");
                        } catch (SQLException e) {

                        }
                    %>
                    <form role="form" class="form-horizontal" action="<%if(i==0)out.print("device_add_do.jsp");else out.print("device_edit_do.jsp?id="+i);%>" method="post">
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">设备名称<sup class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="itemId" name="name"
                                       value="<%=name%>" placeholder="请输入设备名称">
                                <span id="nameError" class="errortext">
                                   <%
                                       name = request.getParameter("name");
                                       if (name != null) {
                                           if (name == "") {
                                               out.println("设备名称不能为空");
                                           } else if (name.length() >= 9) {
                                               out.println("设备名称不能超过8位");
                                           }
                                       }
                                   %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">设备编号<sup class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="number" name="number"
                                       value="<%=number%>"
                                       placeholder="请输入设备编号（由1位英文字母及9位数字组成）">
                                <span id="numberError" class="errortext">
                                  <%
                                      number = request.getParameter("number");
                                      if (number != null) {
                                          if (number == "") {
                                              out.println("设备编号不能为空");
                                          } else if (!number.matches("^[a-zA-Z]\\d{9}$")) {
                                              out.println("设备编号由1位英文字母及9位数字组成");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">设备类型<sup class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <select class="form-control" id="type" name="type">
                                    <option value="">请选择</option>
                                    <option value="重型" <% if (type.equals("重型"))
                                        out.print("selected"); %> >重型
                                    </option>
                                    <option value="中型" <% if (type.equals("中型")) out.print("selected"); %> >
                                        中型
                                    </option>
                                    <option value="轻型" <% if (type.equals("轻型")) out.print("selected"); %> >
                                        轻型
                                    </option>
                                    <option value="便携式" <% if (type.equals("便携式")) out.print("selected"); %> >
                                        便携式
                                    </option>
                                    <option value="其它" <% if (type.equals("其它")) out.print("selected"); %> >
                                        其它
                                    </option>
                                </select>
                                <span id="typeError" class="errortext">
                                  <%
                                      type = request.getParameter("type");
                                      if (type != null) {
                                          if (type == "") {
                                              out.println("请选择设备类型");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">供应商名称<sup
                                    class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="vendor" name="vendor"
                                       value="<%=vendor%>"
                                       placeholder="请输入供应商名称">
                                <span id="vendorError" class="vendorNametext">
                                  <%
                                      vendor = request.getParameter("vendor");
                                      if (vendor != null) {
                                          if (vendor == "") {
                                              out.println("供应商名称不能为空");
                                          } else if (vendor.length() >= 21) {
                                              out.println("供应商名称不能超过20位");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">出厂日期<sup
                                    class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="date" class="form-control" id="date" name="date"
                                       value="<%=date%>">
                                <span id="dateError" class="errortext">
                                  <%
                                      date = request.getParameter("date");
                                      if (date != null) {
                                          if (date == "") {
                                              out.println("请选择出厂日期");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">质保期<sup class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="time" name="time"
                                       value="<%=time%>" placeholder="请输入质保期，以天、月、年为单位">
                                <span id="timeError" class="errortext">
                                  <%
                                      time = request.getParameter("time");
                                      if (time != null) {
                                          if (time == "") {
                                              out.println("质保期不能为空");
                                          } else if (!time.matches("^[0-9]*[天月年]")) {
                                              out.println("请输入以天、月或年为单位的质保期");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label">售后联系人<sup class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="afterSales" name="afterSales"
                                       value="<%=afterSales%>"
                                       placeholder="请输入售后联系人姓名">
                                <span id="afterSalesError" class="errortext">
                                  <%
                                      afterSales = request.getParameter("afterSales");
                                      if (afterSales != null) {
                                          if (afterSales == "") {
                                              out.println("售后联系人不能为空");
                                          } else if (afterSales.length() >= 9) {
                                              out.println("联系人名称不能超过8位");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">联系电话<sup
                                    class="text_sup">*</sup></label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="phone" name="phone"
                                       value="<%=phone%>"
                                       placeholder="请输5五位或11位手机号">
                                <span id="phoneError" class="errortext">
                                  <%
                                      phone = request.getParameter("phone");
                                      if (phone != null) {
                                          if (phone == "") {
                                              out.println("手机号不能为空");
                                          } else if (phone.length() != 5 && phone.length() != 11) {
                                              out.println("手机号为5位或11位");
                                          } else if (!phone.matches("^[0-9]*$")) {
                                              out.println("手机号全为数字");
                                          }
                                      }
                                  %>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">质保说明</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="illustrate" name="illustrate"
                                       value="<%=illustrate%>"
                                       placeholder="请输入质保说明">

                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-primary">保存</button>
                                <button type="reset" class="btn btn-default">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</body>

</html>
